package com.graduationdesign.zengyan.graduationdesignapp.ui.activities.commonlyUsed;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by ZengYan on 2018/5/5.
 *
 * @desc :
 */
public abstract class SlideCardAdapter<T> extends PagerAdapter  {

    private LayoutInflater layoutInflater;
    private int mItemLayout;
    private final List<T> mData;

    public SlideCardAdapter(@Nullable Activity activity,@Nullable @LayoutRes int itemLayout, @Nullable List<T> dataList) {
        layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mItemLayout = itemLayout;
        this.mData = dataList;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = layoutInflater.inflate(mItemLayout, container, false);
        bind(view,mData.get(position),position);
        container.addView(view);
        return view;
    }

    public abstract void bind(View rootview, T data, int position);

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == obj;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }
}
